<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">

      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
		      <el-form-item prop="search" :label="$t('common.search')">
            <el-input
                v-model="state.queryForm.search"
                :placeholder="$t('common.pleaseInput')"
                clearable
                @clear="getDataList"
                style="width: 350px"
                @keydown.enter.prevent="getDataList"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>

      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            v-auth="'bizs_UserVisitorRecord_add'">
            {{ $t('common.addBtn') }}
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
            v-auth="'bizs_UserVisitorRecord_del'" @click="handleDelete(selectObjs)">
            {{ $t('common.delBtn') }}
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'bizs_UserVisitorRecord_export'"
                @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
	  
	  	<fast-table
          ref="fastTable"
          :options="fastTableOptions"
          :data="state.dataList"
          :loading="state.loading"
          @selection-change="selectionChangHandle"
          @sort-change="sortChangeHandle"
      />

      <pagination v-bind="state.pagination" @size-change="sizeChangeHandle" @current-change="currentChangeHandle"/>
      
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemUserVisitorRecord">
import FastTable, {FastTableOptions} from "/@/components/biz/fast-table.vue";
import { BasicTableProps, useTable } from "/@/hooks/table";
import { useMessage, useMessageBox } from "/@/hooks/message";
import { getPage, delObjs } from "/@/api/bizs/UserVisitorRecord";

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// 定义查询字典
import { useDict } from '/@/hooks/dict';

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: getPage
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

// 表格组件实例
const fastTable = ref()
// 表格组件配置项
const fastTableOptions = reactive<FastTableOptions>({
  tableStyle: tableStyle,
  columns: [
	  { prop: 'uid', label: t('UserVisitorRecord.uid'), showOverflowTooltip: true, sortable: "custom" },
    { createInfo: true },
    { updateInfo: true },
    {
      action: {
        display: 'tile',
        buttons: [
          {
            label: t('common.editBtn'),
            icon: 'edit-pen',
            auth: 'bizs_UserVisitorRecord_edit',
            click: (row: any) => formDialogRef.value.openDialog(row.id)
          },
          {
            label: t('common.delBtn'),
            icon: 'delete',
            auth: 'bizs_UserVisitorRecord_del',
            click: (row: any) => handleDelete(row.id)
          },
        ]
      }
    }
  ],

})

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/bizs/userVisitorRecord/export', Object.assign(state.queryForm, { ids: selectObjs }), 'UserVisitorRecord.xlsx')
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({ id }) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm(t('common.delConfirmText'));
  } catch {
    return;
  }

  try {
    const {ok, msg} = await delObjs(ids);
	if (ok) {
		getDataList();
		useMessage().success(t('common.delSuccessText'));
    } else {
      useMessage().error(msg)
    }
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>